<template>
	<view>
		<!-- 选择收货地址的盒子 -->
		<!-- 这里要将addressInfo字段转为JSON字符串再与'{}'进行全等比较 -->
		<view class="choose-address-box" v-if="JSON.stringify(addressInfo)==='{}'">
			<button type="primary" size="mini" @click="chooseAddress">请选择收货地址+</button>			
		</view>
		<!-- 收货信息盒子 -->
		<view class="address-info-box" v-else @click="chooseAddress">
			<view class="header">
				<view class="left">
					收货人:<text>{{addressInfo.userName}}</text>
				</view>
				<view class="right">
					<view class="phone">电话:<text>{{addressInfo.telNumber}}</text></view>
					<uni-icons type="right" size="16" />
				</view>
			</view>
			<view class="footer">
				<view class="address">收货地址:&nbsp;<text>{{addressStr}}</text></view>
			</view>
		</view>
		<!-- 底部的边框线盒子 -->
		<image src="@/static/cart_border@2x.png" class="address-bottom"></image>
	</view>
</template>

<script>
	import {mapState,mapMutations,mapGetters} from "vuex"
	export default {
		name:"my-address",
		data() {
			return {
				// addressInfo: {}
			};
		},
		computed: {
			...mapState("userAbout",["addressInfo"]),		
			...mapGetters("userAbout",["addressStr"])
		},
		methods: {
			...mapMutations("userAbout",["updateAddressInfo"]),
			async chooseAddress() {
				// 返回的数组中第一个参数是错误对象，第二个参数是成功之后的收货地址对象
				let [err,succ]= await uni.chooseAddress()
				if(err===null && succ.errMsg==="chooseAddress:ok") {
					// this.addressInfo=succ
					this.updateAddressInfo(succ)
				}
			}
		}
	}
</script>

<style lang="scss">
.choose-address-box {
	height: 90px;
	display: flex;
	justify-content: center;
	align-items: center;
}
.address-info-box {
	font-size: 12px;
	height: 90px;
	display: flex;
	flex-direction: column;
	justify-content: center;
	padding: 0 5px;
	
	.header {
		display: flex;
		justify-content: space-between;
		
		.right {
			display: flex;
			align-items: center;
			
			.phone {
				margin-right: 5px;
			}
		}
	}
	
	.footer {
		display: flex;
		align-items: center;
		margin-top: 10px;
	}
}
.address-bottom {
	display: flex;
	width: 100%;
	height: 5px;
}
</style>